package com.simple.trade.controller.vm;

import com.simple.common.entity.ResData;
import com.simple.common.entity.controller.BaseController;
import com.simple.common.entity.req.vm.pay.DirectPayReqV1;
import com.simple.common.entity.req.vm.pay.DirectPayReqV2;
import com.simple.common.entity.req.vm.pay.RedirectPayReqV1;
import com.simple.common.entity.req.vm.pay.RedirectPayReqV2;
import com.simple.common.entity.vo.pay.DirectResV1;
import com.simple.common.entity.vo.pay.DirectResV2;
import com.simple.common.entity.vo.pay.RedirectResV1;
import com.simple.common.entity.vo.pay.RedirectResV2;
import com.simple.trade.holder.PayContext;
import com.simple.trade.service.vm.DirectPayV1Service;
import com.simple.trade.service.vm.DirectPayV2Service;
import com.simple.trade.service.vm.RedirectPayService;
import com.simple.trade.service.vm.RedirectPayV2Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import static com.simple.common.entity.constant.Payment.*;


/**
 * @author yasud
 * @date 2020年09月22日 15:41
 * @description
 */
@RestController
public class PayController extends BaseController {

    final
    DirectPayV1Service directPayV1Service;

    final
    DirectPayV2Service directPayV2Service;

    final
    RedirectPayService redirectPayService;

    final
    RedirectPayV2Service redirectPayV2Service;

    public PayController(DirectPayV1Service directPayV1Service, DirectPayV2Service directPayV2Service, RedirectPayService redirectPayService, RedirectPayV2Service redirectPayV2Service) {
        this.directPayV1Service = directPayV1Service;
        this.directPayV2Service = directPayV2Service;
        this.redirectPayService = redirectPayService;
        this.redirectPayV2Service = redirectPayV2Service;
    }


    /**
     * 直连支付V1
     * @param req
     * @return
     */
    @PostMapping("/v1/directPay")
    public ResData<DirectResV1> directPayV1(@RequestBody @Validated DirectPayReqV1 req) {
        PayContext.current().iam(req.getAppId(), VM_DIRECT_PAY_CODE);
        req.check();
        ResData<DirectResV1> directResV1ResData = directPayV1Service.directPayV1(req);
        return directResV1ResData;
    }

    /**
     * 直连支付V2
     * @param req
     * @return
     */
    @PostMapping("/v2/directPay")
    public ResData<DirectResV2> directPayV2(@RequestBody @Validated DirectPayReqV2 req) {
        PayContext.current().iam(req.getAppId(), VM_DIRECT_PAY_2_CODE);
        req.check();
        ResData<DirectResV2> directResV1ResData = directPayV2Service.directPayV2(req);
        return directResV1ResData;
    }

    /**
     * 跳转支付
     * @param req
     * @return
     */
    @PostMapping("/v1/cashier")
    public ResData<RedirectResV1> redirectPay(@RequestBody @Validated RedirectPayReqV1 req) {
        PayContext.current().iam(req.getAppId(), VM_REDIRECT_PAY_CODE);
        req.check();
        ResData<RedirectResV1> redirectResV1ResData = redirectPayService.redirectPayV1(req);
        return redirectResV1ResData;
    }

    /**
     * 跳转支付
     * @param req
     * @return
     */
    @PostMapping("/v2/cashier")
    public ResData<RedirectResV2> redirectPay2(@RequestBody @Validated RedirectPayReqV2 req) {
        PayContext.current().iam(req.getAppId(), VM_REDIRECT_PAY_2_CODE);
        req.check();
        ResData<RedirectResV2> redirectResV1ResData = redirectPayV2Service.redirectPayV2(req);
        return redirectResV1ResData;
    }

}
